package com.yakaten.login;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.CookieHandler;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.seasar.framework.log.Logger;
import org.seasar.framework.util.ResourceUtil;

import com.yakaten.exception.KakakuException;

public class KakakuLogin {

	static Logger logger = Logger.getLogger(KakakuLogin.class);

	public boolean doLoginStep0(String userName, String password) throws KakakuException {
		OutputStreamWriter out = null;
		BufferedReader bufferedReader = null;
		HttpURLConnection connection = null;
		try {
			String LOGIN_URL0 = ResourceUtil
					.getProperties("yakaten.properties").getProperty(
							"kakaku.login.url0");
			URL loginUrl = new URL(LOGIN_URL0);
			connection = (HttpURLConnection) loginUrl.openConnection();
			connection.setInstanceFollowRedirects(true);
			connection.setDoOutput(true);

			out = new OutputStreamWriter(connection.getOutputStream(),
					"shift_jis");
			out.write("login=" + userName + "&pswd=" + password
					+ "&BtnLogin.x=44&BtnLogin.y=4");

			out.flush();
			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, "shift_jis"));
			String ss = null;
			StringBuffer total = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				total.append(ss);
			}

			if (logger.isDebugEnabled()) {
				logger.debug(total);
			}
		} catch (MalformedURLException e) {
			throw new KakakuException(e);
		} catch (IOException e) {
			throw new KakakuException(e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}

		return true;
	}

	public boolean doLoginStep1() throws KakakuException {
		boolean result = false;
		BufferedReader bufferedReader = null;
		HttpURLConnection connection = null;
		try {
			String LOGIN_URL1 = ResourceUtil
					.getProperties("yakaten.properties").getProperty(
							"kakaku.login.url1");
			URL loginUrl = new URL(LOGIN_URL1);

			connection = (HttpURLConnection) loginUrl.openConnection();
			connection.connect();

			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, "shift_jis"));
			String ss = null;
			StringBuffer total = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				total.append(ss);
			}

			if (logger.isDebugEnabled()) {
				logger.debug(total);
			}

			if (total.indexOf("管理画面") > -1) {
				result = true;
			}

		} catch (MalformedURLException e) {
			throw new KakakuException(e);
		} catch (IOException e) {
			throw new KakakuException(e);
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}

		return result;
	}

	public boolean doLogin(String userName, String password) throws KakakuException {
		CorrectedCookieManager manager = new CorrectedCookieManager();
		manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
		CookieHandler.setDefault(manager);
		return this.doLoginStep0(userName, password) && this.doLoginStep1();
	}

	public static void main(String[] args) {
		KakakuLogin login = new KakakuLogin();
		try {
			logger.debug(login.doLogin("tydkenms", "38srka2b"));
		} catch (KakakuException e) {
			e.printStackTrace();
		}
	}
}
